package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.service.ICollectService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("collects")
public class CollectController {
    @Autowired
    private ICollectService collectService;

    /**
     * 获取所有收藏游记
     *
     * @param qo
     * @return
     */
    @RequestMapping(value = "/{userId}/travels", method = RequestMethod.GET)
    public PageInfo queryForTravel(CollectQueryObject qo) {
        return collectService.queryForTravel(qo);
    }

    /**
     * 获取所有收藏日报
     *
     * @param qo
     * @return
     */
    @RequestMapping(value = "/{userId}/news", method = RequestMethod.GET)
    public PageInfo queryForNews(CollectQueryObject qo) {
        return collectService.queryForNews(qo);
    }

    /**
     * 查询游记或日报是否已经收藏
     *
     * @param qo
     * @return
     */
    @RequestMapping(value = "/travels", method = RequestMethod.GET)
    public Collect selectByTravelId(CollectQueryObject qo) {
        return collectService.selectById(qo);
    }

    /**
     * 取消收藏游记
     *
     * @param qo
     * @return
     */
    @RequestMapping(value = "/travels", method = RequestMethod.DELETE)
    public JSONResult deleteTravels(CollectQueryObject qo) {
        JSONResult result = new JSONResult();
        //取消收藏
        try {
            if (qo.getNewsId() == null) {
                collectService.delete(qo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 收藏游记
     *
     * @param qo
     * @return
     */
    @RequestMapping(value = "/travels", method = RequestMethod.PUT)
    public JSONResult saveTravels(CollectQueryObject qo) {
        JSONResult result = new JSONResult();
        //收藏游记
        try {
            collectService.saveTravel(qo);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
